<?php

	namespace Phptricks\app\controllers\ajax;

	/**
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 */
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\app\controllers\base\base as base;
	use Phptricks\system\library\session\session as session;
	use Phptricks\system\library\upload\upload as upload;
	use Phptricks\system\library\models\models as models;
	use Phptricks\app\models\users\users as users_model;

	class ajax extends base{

		public $_session = null;
		public $upload;
		private $model = null;
		private $users_model = null;
		private $user_fields = null;

		public function __construct(){
			parent::__construct();
			$this->model = models::getinstance();
			$this->user_fields = users_model::getinstance();
			$this->user_fields = array(
				'user_id',
				'user_group_id',
				'user_country_id',
				'user_name',
				'user_fullname',
				'user_email',
				'user_stop',
				'user_date_registering',
				'user_fields',
				'count_tried',
				'count_comment',
				'reputation'
			);
		}

		/**
		 * 
		 * @param String $dir
		 */
		public function upload($dir = ''){
			if (empty($dir)) {
				$dir = 'other';
			}
			$this->upload = upload::getinstance();
			$files = $this->form_validation->_files;
			$upload_dir = $this->config->website->upload_dir . $dir . "/";
			$array_return = $this->upload->uploads('', $upload_dir, false);
			$files_list = $this->custom_fields->encode($array_return);
			die($files_list);
		}

		public function logout(){
			$this->_session->delete('facebook');
		}

		public function login(){
			$this->_session->delete('facebook');
		}

		public function check_user_data($field, $data){
			if (!isset($field) || empty($field) || !isset($data) || empty($data)) {
				echo 'false';
				exit();
			}

			$data_return = '';
			if (!\in_array($field, $this->user_fields)) {
				echo 'false';
				exit();
			}
			$where_array = array(
				$field => $data
			);
			$users_list = $this->users_model->get_users_by_data($where_array);
			$data_return = array(
				'count' => $this->users_model->_count_row,
				'data' => $users_list,
			);
			$data_return = $this->custom_fields->encode($data_return);
			echo $data_return;
		}

	}
	